package com.whfp.coky.ui.main.frag

import android.content.Context
import android.os.Bundle
import android.view.KeyEvent
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.whfp.coky.databinding.FragFixedBinding
import com.whfp.coky.databinding.ItemFixedTaskBinding
import com.whfp.coky.event.EventCode
import com.whfp.coky.event.EventMessage
import com.whfp.coky.other.Constants
import com.whfp.coky.ui.base.BaseAdapter
import com.whfp.coky.ui.base.BaseRecycleFrag
import com.whfp.coky.ui.main.DuggerTaskCountAct
import com.whfp.coky.ui.main.model.DruggerBean
import com.whfp.coky.ui.main.model.TaskCount01Bean
import com.whfp.coky.ui.main.vm.TaskFixedFragVM

/**
 *首页frag01
 * 固定任务frag
 *
 * created by coky on 2022/3/3
 */
class TaskFixedFrag: BaseRecycleFrag<TaskFixedFragVM,FragFixedBinding,DruggerBean>(){

    companion object{
        fun newInstance(): TaskFixedFrag{
            return TaskFixedFrag()
        }
    }

    /**
     * 继承baseRecyclefrag需要相关的组件绑定到实现类的binding上
     * 初始化适配器，适配器写在内部内中，主要实现界面绘制和界面交互的点击事件
     */
    override fun initAdapter(context: Context) = FixedAdapter(context)

    override fun initRecycler() = vBinding.rvRecycler

    override fun initRefreshLayout() = vBinding.refreshLayout


    /**
     * 需要super.initView()完成父类中recycle相关view的初始化
     * 额外加上实现类的相关组件初始化
     */
    override fun initView() {
        super.initView()
//        监听输入。和隐藏
        vBinding.etSearch.let {
            it.setOnEditorActionListener(object : TextView.OnEditorActionListener{
                override fun onEditorAction(tv: TextView?, action: Int, event: KeyEvent?): Boolean {
                    if (action == EditorInfo.IME_ACTION_SEARCH){
                        hideKeyboard(tv)
                        vModel.changeReq(it.text.toString())
                        return true
                    }
                    return false
                }

            })
        }
    }

    /**
     * 获取列表中
     */
    fun initTaskCount(count01Bean: TaskCount01Bean?) {
        count01Bean?.let {
            vBinding.tvWc.text = it.completedNum.toString()
            vBinding.tvDb.text = it.executedNum.toString()
            vBinding.tvGq.text = it.expiredNum.toString()
        }
    }


    inner class FixedAdapter(mContext:Context): BaseAdapter<ItemFixedTaskBinding,DruggerBean>(mContext){
        override fun convert(v: ItemFixedTaskBinding, t: DruggerBean, position: Int) {
            v.tvName.text = t.realName
            v.tvSex.text = t.sex
            v.tvType.text = t.controlStatus
            v.tvDepartment.text = t.orgName
            v.tvPhone.text = t.phone
            v.tvDate.text = t.createTime
            v.tvFxdj.text = when(t.level){
                "1" -> "高风险"
                "2" -> "中风险"
                "3" -> "低风险"
                "4" -> "无风险"
                else -> "未分级"
            }
        }
    }

    override fun itemClick(pos: Int) {
        val b = Bundle().apply {
            this.putSerializable(Constants.BEAN, vModel.mAdapter?.getItem(pos))
        }
        vModel.mAdapter?.getItem(pos)
        getAttachActivity()?.startActivity(DuggerTaskCountAct::class.java,b)
    }

    override fun handleEvent(msg: EventMessage) {
        super.handleEvent(msg)
        when(msg.code){
            EventCode.SCAN_CODE -> toast("fixedfrag收到结果${msg.msg}")
        }
    }
}


